#include "convertor.h"
#include "cmdutil.h"
#include <QDir>
#include <QCoreApplication>
#include <QDebug>
Convertor::Convertor(QObject *parent)
    : QObject{parent}
{

}

Convertor::Convertor(QObject *parent, QString dllPath,QString platform): QObject{parent}
{
    QFileInfo dllFileInfo(dllPath);
    this->setDllPath(dllFileInfo.absoluteFilePath());
    this->setFileBaseName(dllFileInfo.completeBaseName());
    this->setPlatform(platform);
}

void Convertor::generateDef()
{
    auto localCurrentPath = QCoreApplication::applicationDirPath();
    QString gendefExePath = localCurrentPath+ "/bin/gendef.exe";
    QString defPath = localCurrentPath+ "/out/"+ this->getFileBaseName()+".def";
    QString defCmd = gendefExePath + " " + this->getDllPath();
    qDebug() << "defCmd:" << defCmd;
    QString standar;
    QString error;
    bool result = CmdUtil::exec(localCurrentPath+ "/out/",defCmd,standar,error);
    if(result){
       this->setDefPath(defPath);
    }

}

void Convertor::pexportDef()
{
    auto localCurrentPath = QCoreApplication::applicationDirPath();
    QString pexportExePath = localCurrentPath+ "/bin/pexports.exe";
    QString defPath = localCurrentPath+ "/out/"+ this->getFileBaseName()+".def";
    QString defCmd = pexportExePath + " " + this->getDllPath() +" -o > " + defPath;
    qDebug() << "defCmd:" << defCmd;
    QString standar;
    QString error;
    bool result = CmdUtil::exec(defCmd,standar,error);
    if(result){
        this->setDefPath(defPath);
    }
}

bool Convertor::buildLibFile()
{
    auto localCurrentPath = QCoreApplication::applicationDirPath();
    QString libExePath = localCurrentPath+ "/bin/lib.exe";
    QString libPath = localCurrentPath+ "/out/"+ this->getFileBaseName() +".lib";
    QString defPath = this->getDefPath();
    QString libCmd = libExePath + " " + "/out:"+ libPath +" /MACHINE:I"+this->getPlatform()+" /DEF:" + defPath;
    qDebug() << "libCmd:" << libCmd;

    QString standar;
    QString error;
    bool result = CmdUtil::exec(libCmd,standar,error);
    return result;

}

bool Convertor::buildAFile()
{
    auto localCurrentPath = QCoreApplication::applicationDirPath();
    QString dllToolExePath = localCurrentPath+ "/bin/dlltool.exe";
    QString aPath = localCurrentPath+ "/out/lib"+ this->getFileBaseName() +".a";
    QString defPath = this->getDefPath();
    QString libCmd = dllToolExePath + " -D " +this->getDllPath() +" -d " + defPath +" -l " + aPath;
    qDebug() << "libCmd:" << libCmd;

    QString standar;
    QString error;
    bool result = CmdUtil::exec(libCmd,standar,error);
    return result;
}



QString Convertor::getDefPath() const
{
    return defPath;
}

void Convertor::setDefPath(const QString &newDefPath)
{
    defPath = newDefPath;
}

QString Convertor::getFileBaseName() const
{
    return fileBaseName;
}

void Convertor::setFileBaseName(const QString &newFileBaseName)
{
    fileBaseName = newFileBaseName;
}

QString Convertor::getPlatform() const
{
    return platform;
}

void Convertor::setPlatform(const QString &newPlatform)
{
    platform = newPlatform;
}

QString Convertor::getDllPath() const
{
    return dllPath;
}

void Convertor::setDllPath(const QString &newDllPath)
{
    dllPath = newDllPath;
}

